//
//  UIBarButtonItem+Factory.m
//  GANGAIKit
//
//  Created by fh on 2019/11/6.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "UIBarButtonItem+Factory.h"
#import "GAKKIConfig.h"
#import "GAKKIBaseButton.h"

@implementation UIBarButtonItem (Factory)

/// 快速创建一个 UIBarButtonItem
/// @param imageName 普通状态下的图片
/// @param highImageName 高亮状态下的图片
/// @param target 目标
/// @param action 事件
+ (UIBarButtonItem *)gakki_itemWithImageName:(NSString *)imageName highImageName:(NSString *)highImageName target:(id)target action:(SEL)action {
    GAKKIBaseButton* button = [[GAKKIBaseButton alloc] init];
    if(imageName) {
       [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
    if(highImageName) {
        [button setImage:[UIImage imageNamed:highImageName] forState:UIControlStateHighlighted];
    }
    button.frame = CGRectMake(0, 0, 100, 44);
    // 监听按钮点击
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    return item;
}

/// 快速创建一个 UIBarButtonItem
/// @param imageName 普通状态下的图片
/// @param target 目标
/// @param action 事件
+ (UIBarButtonItem *)gakki_itemWithImageName:(NSString *)imageName target:(id)target action:(SEL)action {
    return [UIBarButtonItem gakki_itemWithImageName:imageName highImageName:nil target:target action:action];
}

/// 通过系统的方法，初始化一个UIBarButtonItem
/// @param title 显示的文字，例如'完成'、'取消'
/// @param titleColor title的颜色，if nil ，The Color is [UIColor whiteColor]
/// @param imageName 图片名称
/// @param target 目标
/// @param selector 事件
/// @param textType 是否是纯文字
+ (UIBarButtonItem *)gakki_systemItemWithTitle:(NSString *)title
                                 titleColor:(UIColor *)titleColor
                                  imageName:(NSString *)imageName
                                     target:(id)target
                                   selector:(SEL)selector
                                   textType:(BOOL)textType {
    UIBarButtonItem *item = textType ?
    ({
        /// 设置普通状态的文字属性
        item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:selector];
        titleColor = titleColor?titleColor:[UIColor blueColor]; //  whiteColor
        NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSForegroundColorAttributeName] = titleColor;
        textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowOffset =  CGSizeZero;
        textAttrs[NSShadowAttributeName] = shadow;
        [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        
        // 设置高亮状态的文字属性
        NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
        highTextAttrs[NSForegroundColorAttributeName] = [titleColor colorWithAlphaComponent:.5f];
        [item setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
        
        // 设置不可用状态(disable)的文字属性
        NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
        disableTextAttrs[NSForegroundColorAttributeName] = [titleColor colorWithAlphaComponent:.5f];
        [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
        
        item;
    }) : ({
        item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:target action:selector];
        item;
    });
    return item;
}

/// 通过自定义的方法，快速初始化一个UIBarButtonItem，内部是按钮
/// @param title 显示的文字，例如'完成'、'取消'
/// @param titleColor title的颜色，if nil ，The Color is [UIColor whiteColor]
/// @param imageName 图片名称
/// @param target 目标
/// @param selector 事件
/// @param contentHorizontalAlignment 文本对齐方向
+ (UIBarButtonItem *)gakki_customItemWithTitle:(NSString *)title
                                 titleColor:(UIColor *)titleColor
                                  imageName:(NSString *)imageName
                                     target:(id)target
                                   selector:(SEL)selector
                 contentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment {
    
    UIButton *item = [[UIButton alloc] init];
    titleColor = titleColor?titleColor:[UIColor whiteColor];
    if (kStringIsNotEmpty(title)) {
        [item setTitle:title forState:UIControlStateNormal];
    }
    if (kStringIsNotEmpty(imageName)) {
        [item setImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    [item.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [item setTitleColor:titleColor forState:UIControlStateNormal];
    [item setTitleColor:[titleColor colorWithAlphaComponent:.5f] forState:UIControlStateHighlighted];
    [item setTitleColor:[titleColor colorWithAlphaComponent:.5f] forState:UIControlStateDisabled];
    [item sizeToFit];
    
    CGRect frame = item.frame;
    frame.size = CGSizeMake(44, 44);
    item.frame = frame;
    
    item.contentHorizontalAlignment = contentHorizontalAlignment;
    [item addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:item];
}

/// 快速创建一个导航栏leftBarButtonItem 用于返回（pop）或者（dismiss），切记只能是纯图片 （eg: < or X）且可以加大点击范围
/// @param title 显示的文字，例如'完成'、'取消'
/// @param imageName 返回按钮的图片
/// @param target 目标
/// @param action 事件
+ (UIBarButtonItem *)gakki_backItemWithTitle:(NSString *)title
                                imageName:(NSString *)imageName
                                   target:(id)target
                                   action:(SEL)action {
    return [self gakki_customItemWithTitle:title titleColor:nil imageName:imageName target:target selector:action contentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
}

@end
